using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VBASTV
    {
        public DataRow Convert2BASTV(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryBASTV(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["BVMANDANT"] = " ".PadRight(2 ); 
         dwReturn["BVKART"] = " ".PadRight(20 ); 
         dwReturn["BVKVAR"] = " ".PadRight(2 ); 
         dwReturn["BVPOS"] = " ".PadRight(2 ); 
         dwReturn["BVAENDZL"] = " ".PadRight(0 ); 
         dwReturn["BVAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["BVAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["BVNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["BVNEUDAT"] = " ".PadRight(0 ); 
         dwReturn["BVANZSB"] = " ".PadRight(0 ); 
         dwReturn["BVFDZ"] = " ".PadRight(0 ); 
         dwReturn["BVLFDZ"] = " ".PadRight(0 ); 
         dwReturn["BVBAHN"] = " ".PadRight(1 ); 
         dwReturn["BVBREITE"] = " ".PadRight(0 ); 
         dwReturn["BVABFALL"] = " ".PadRight(0 ); 
         dwReturn["BVLLG"] = " ".PadRight(0 ); 
         dwReturn["BVAUFM"] = " ".PadRight(2 ); 
         dwReturn["BVBEM"] = " ".PadRight(15 ); 
         dwReturn["BVFILLER"] = " ".PadRight(62 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapBASTV(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveBASTV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBASTV(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapBASTV(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertBASTV(dwLocal, ITXDBVERSION);
            }
            UpdateBASTV(dwLocal);
        }
        public void InsertBASTV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO BASTV(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryBASTV(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BASTV WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM BASTV  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "BASTV";
            Return = Result;
            return Return;
        }
        public void DeleteBASTV(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM BASTV   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateBASTV(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE BASTV SET "
                    +"BVMANDANT= :BVMANDANT,"
                    +"BVKART= :BVKART,"
                    +"BVKVAR= :BVKVAR,"
                    +"BVPOS= :BVPOS,"
                    +"BVAENDZL= :BVAENDZL,"
                    +"BVAENDBEN= :BVAENDBEN,"
                    +"BVAENDDAT= :BVAENDDAT,"
                    +"BVNEUBEN= :BVNEUBEN,"
                    +"BVNEUDAT= :BVNEUDAT,"
                    +"BVANZSB= :BVANZSB,"
                    +"BVFDZ= :BVFDZ,"
                    +"BVLFDZ= :BVLFDZ,"
                    +"BVBAHN= :BVBAHN,"
                    +"BVBREITE= :BVBREITE,"
                    +"BVABFALL= :BVABFALL,"
                    +"BVLLG= :BVLLG,"
                    +"BVAUFM= :BVAUFM,"
                    +"BVBEM= :BVBEM,"
                    +"BVFILLER= :BVFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("BVMANDANT", OracleType.NVarChar).Value = dwData["BVMANDANT"].ToString(); 
          or1.Parameters.Add("BVKART", OracleType.NVarChar).Value = dwData["BVKART"].ToString(); 
          or1.Parameters.Add("BVKVAR", OracleType.NVarChar).Value = dwData["BVKVAR"].ToString(); 
          or1.Parameters.Add("BVPOS", OracleType.NVarChar).Value = dwData["BVPOS"].ToString(); 
          or1.Parameters.Add("BVAENDZL", OracleType.NVarChar).Value = dwData["BVAENDZL"].ToString(); 
          or1.Parameters.Add("BVAENDBEN", OracleType.NVarChar).Value = dwData["BVAENDBEN"].ToString(); 
          or1.Parameters.Add("BVAENDDAT", OracleType.NVarChar).Value = dwData["BVAENDDAT"].ToString(); 
          or1.Parameters.Add("BVNEUBEN", OracleType.NVarChar).Value = dwData["BVNEUBEN"].ToString(); 
          or1.Parameters.Add("BVNEUDAT", OracleType.NVarChar).Value = dwData["BVNEUDAT"].ToString(); 
          or1.Parameters.Add("BVANZSB", OracleType.NVarChar).Value = dwData["BVANZSB"].ToString(); 
          or1.Parameters.Add("BVFDZ", OracleType.NVarChar).Value = dwData["BVFDZ"].ToString(); 
          or1.Parameters.Add("BVLFDZ", OracleType.NVarChar).Value = dwData["BVLFDZ"].ToString(); 
          or1.Parameters.Add("BVBAHN", OracleType.NVarChar).Value = dwData["BVBAHN"].ToString(); 
          or1.Parameters.Add("BVBREITE", OracleType.NVarChar).Value = dwData["BVBREITE"].ToString(); 
          or1.Parameters.Add("BVABFALL", OracleType.NVarChar).Value = dwData["BVABFALL"].ToString(); 
          or1.Parameters.Add("BVLLG", OracleType.NVarChar).Value = dwData["BVLLG"].ToString(); 
          or1.Parameters.Add("BVAUFM", OracleType.NVarChar).Value = dwData["BVAUFM"].ToString(); 
          or1.Parameters.Add("BVBEM", OracleType.NVarChar).Value = dwData["BVBEM"].ToString(); 
          or1.Parameters.Add("BVFILLER", OracleType.NVarChar).Value = dwData["BVFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
